package com.lumyc.store.service.impl;

import com.lumyc.store.entity.User;
import com.lumyc.store.mapper.UserMapper;
import com.lumyc.store.service.UserService;
import com.lumyc.store.service.ex.UserDuplicateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User findUserByUid(String uid) {
        return userMapper.findUserByUid(uid);
    }

    @Override
    public void register(User user) {
        User result = userMapper.findUserByUsername(user.getUsername());
        if(result != null){
            throw new UserDuplicateException("用户已存在！");
        }
        user.setCreatedUser(user.getUsername());
        user.setCreatedTime(new Date());
        user.setModifiedUser(user.getUsername());
        user.setModifiedTime(new Date());
        userMapper.insertUser(user);
        User userByUsername = userMapper.findUserByUsername(user.getUsername());
        user.setUid(userByUsername.getUid());
    }

    @Override
    public List<User> findAllUsers() {
        return userMapper.findAllUsers();
    }
}
